<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <my-search @clickXxx="gotoSearch"></my-search>
    </view>
    
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item, i) in swiperData" :key="i">
        <navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 菜单导航 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item, i) in navList" :key="i" @click="navClickHandler(item)">
        <image :src="item.image_src"></image>
      </view>
    </view>
    <!-- 楼层数据 -->
    <view class="floor-list">
      <view class="floor-item" v-for="(item, i) in floorList" :key="i">
        <!-- floor title -->
        <image class="floor-title" :src="item.floor_title.image_src"></image>
        <view class="image-box">
            <!-- 左边floor-image 1张 -->
            <view class="left-image-box">
              <navigator :url="item.product_list[0].url">
                <image
                  :src="item.product_list[0].image_src"
                  :style="{width: item.product_list[0].image_width+'rpx'}"
                  mode="widthFix"
                >
                </image>
              </navigator>
            </view>
            <!-- 右边floor-image 4张 -->
            <view class="right-image-box">
              <navigator :url="item2.url" v-for="(item2, i2) in item.product_list" v-if="i2 !==0">
                <image
                  :style="{width: item2.image_width+'rpx'}"
                  :src="item2.image_src"
                  mode="widthFix"
                  :key="i2"
                >
                </image>
              </navigator>
            </view>
          </view>
      </view>
      
    </view>
    
  </view>
</template>

<script>
  
  import badgeMix from '@/mixins/tabbar-badge.js'
  
  export default {
    mixins: [badgeMix], 
    data() {
      return {
        swiperData: [], //轮播图数据
        navList: [], // 菜单导航数据
        floorList: [], // 楼层数据
      };
    },
    onLoad() {
      // 发起请求获取轮播图数据
      this.getSwiperData()
      // 发起菜单导航数据请求
      this.getNavList()
      // 发起楼层数据请求
      this.getFloorList()
    },
    methods: {
      // 获取轮播图数据
      async getSwiperData(){
        const { data: res } = await uni.$http.get('/home/swiperdata')
        console.log(res)
        // 异常数据判断
        if(res.meta.status !== 200) return uni.$showMsg()
        // if(res.meta.status !== 200) {
        //   return uni.showToast({
        //     title: '数据请求异常',
        //     icon: 'none'
        //   })
        // }
        // 设置页面状态
        this.swiperData = res.message
      },
      // 获取菜单导航数据
      async getNavList(){
        const { data: res } = await uni.$http.get('/home/catitems')
        console.log(res)
        // 异常数据判断
        if(res.meta.status !== 200) return uni.$showMsg()
        // 设置页面状态
        this.navList = res.message
      },
      // 菜单栏导航点击处理函数
      navClickHandler(e){
        console.log(e)
        // 跳转到分类tab页面
        if(e.name === '分类'){
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      // 获取楼层数据
      async getFloorList(){
        const {data: res} = await uni.$http.get('/home/floordata')
        console.log(res)
        // 异常数据判断
        if(res.meta.status !== 200) return uni.$showMsg()
        // 设置页面数据
        // 处理返回的数据
        res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            // 按需拼接字符串
            prod.url = '/subpkg/goods_list/goods_list?'+prod.navigator_url.split('?')[1]
          })
        })
        
        this.floorList = res.message
        
      },
      // 跳转到搜索页面
      gotoSearch(){
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
      
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 9;
  }
  
  swiper {
    height: 330rpx;
    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }
  .nav-list {
    display: flex;
    justify-content: space-around;
    padding-top: 24rpx;
    .nav-item image{
      width: 128rpx;
      height: 140rpx;
    }
  }
  .floor-list {
    .floor-item {
      .floor-title {
        width: 100%;
        height: 60rpx;
      }
      .image-box {
        display: flex;
        .left-image-box {
          margin-left: 8rpx;
        }
        .right-image-box {
          display: flex;
          flex-wrap: wrap;
          justify-content: space-around;
        }
      }
    }
  }
</style>
